package com.ruoyi.hr.service.impl.changemanage.internshipchangejust;

import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.hr.domain.Authority;
import com.ruoyi.hr.mapper.SkyMsgAlarmMapper;
import com.ruoyi.hr.mapper.changemanage.ChangeManageMapper;
import com.ruoyi.hr.mapper.changemanage.internshipchangejust.InternshipChangeJustMapper;
import com.ruoyi.hr.service.changemanage.internshipchangejust.InternshipChangeJustService;
import com.ruoyi.hr.util.BaseResult;
import com.ruoyi.hr.util.ResultEnum;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Service
public class InternshipChangeJustServiceImpl implements InternshipChangeJustService {

    @Autowired
    InternshipChangeJustMapper internshipChangeJustMapper;

    @Autowired
    ChangeManageMapper changeManageMapper;

    @Autowired
    SysUserMapper sysUserMapper;

    @Autowired
    SkyMsgAlarmMapper skyMsgAlarmMapper;

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public List<Map<String, Object>> findInternshipChangeJustListData(Map map, Authority authority) {
        return internshipChangeJustMapper.findInternshipChangeJustListData(map, authority);
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public List<Map<String, Object>> findInternshipChangeJustEmp(Map map, Authority authority) {
        return internshipChangeJustMapper.findInternshipChangeJustEmp(map, authority);
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public BaseResult addInternshipChangeJustEmp(String allEid) {
        //叠加异常信息
        String msgAdd = "";
        try {
            //存储过程sql
            String sql;
            SysUser user = ShiroUtils.getSysUser();
            Map<String, Object> maps = sysUserMapper.selectSkySecUser(user.getLoginName());
            String[] array = allEid.split(",");
            String msg;//异常信息
            for (String s : array) {
                //根据PEID取出人名
                String name = changeManageMapper.getEmpName(s);
                sql = "declare @RetVal varchar(50) exec eSP_PracAddEmp " + s + ",2," + maps.get("id").toString() + ",@RetVal output select @RetVal";
                String retVal = internshipChangeJustMapper.executeProcedure(sql);
                msg = skyMsgAlarmMapper.getMsgById(retVal);
                if (msg != null) {
                    msgAdd += name + ":" + msg + "\n";
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return BaseResult.build(ResultEnum.SERVER_ERROR);
        }
        if ("".equals(msgAdd)) {
            return BaseResult.build(ResultEnum.SUCCESS);
        } else {
            return BaseResult.build(19970926, msgAdd, null);
        }
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public Map<String, Object> findInternshipChangeJustData(String id) {
        Map<String, Object> JustEmp = internshipChangeJustMapper.findInternshipChangeJustData(id);
        //根据depId找出对应岗位集合
        List<Map<String, Object>> jobList = internshipChangeJustMapper.findJobList(JustEmp.get("depId").toString());
        JustEmp.put("jonList", jobList);
        return JustEmp;
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public int editInternshipJustEmp(Map map) {
        return internshipChangeJustMapper.editInternshipJustEmp(map);
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public List<Map<String, Object>> historyList(Map map, Authority authority) {
        return internshipChangeJustMapper.historyList(map, authority);
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public int removeLargeTwoMonths() {
        //设置日期格式
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        int count = 0;
        try {
            //1.先取出所有入职登记的数据(还没有确认检查),取出每个登记时间的第二个月作为过期日期
            //2.取出当前时间,再拿后两个月的数据和当前时间比较,如果后者大于前者，则删除
            List<Map<String, Object>> empDataList = internshipChangeJustMapper.findEmpDataAll();
            for (Map map : empDataList) {
                //循环取出每一条记录的过期日期
                String expirationDate = internshipChangeJustMapper.expirationDate(map.get("RegDate").toString());
                //取出当前日期,如果当前日期比过期日期大，则删除
                String currentDate = df.format(new Date());
                if (df.parse(currentDate).after(df.parse(expirationDate))) {
                    //如果当前日期比过期日期大,则过期,即删除
                    String id = map.get("id").toString();
                    count += internshipChangeJustMapper.removeInternshipChangeJust(id);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return count;
    }
}
